package com.sservice.service.service.impl;

import com.sservice.model.dao.SaleLevelMapper;
import com.sservice.model.entity.SaleLevel;
import com.sservice.service.service.ISaleLevelService;
import com.sservice.base.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by suweiming on 2017/12/20.
 */


@Service
public class SaleLevelServiceImpl implements ISaleLevelService {

    @Autowired
    private SaleLevelMapper dao;

    public Map<String, List<SaleLevel>> getLevelObj() {
        List<SaleLevel> list = this.selectAll();
        Map<String, List<SaleLevel>> levelObj = list.stream().collect(Collectors.groupingBy((SaleLevel::getLabel)));
        return levelObj;
    }

    /**
     * 会员级别
     * 根据会员成长值获取会员级别
     *
     * @return
     */
    public Map getLevel(String jifen) {
        Map map = new HashMap();
        Map<String, List<SaleLevel>> levelObj = getLevelObj();
        if (StringUtils.isNoneBlank(jifen)) {
            int jifenCount = Integer.valueOf(jifen);
            if (jifenCount >= 1 && jifenCount < levelObj.get("绿卡").get(0).getLevel()) {
                map.put("url", levelObj.get("绿卡").get(0).getUrl());
                map.put("level", "绿卡");
                map.put("percent", levelObj.get("绿卡").get(0).getPercent());
            } else if (jifenCount >= levelObj.get("绿卡").get(0).getLevel() && jifenCount < levelObj.get("银卡").get(0).getLevel()) {
                map.put("url", levelObj.get("银卡").get(0).getUrl());
                map.put("level", "银卡");
                map.put("percent", levelObj.get("银卡").get(0).getPercent());

            } else if (jifenCount >= levelObj.get("银卡").get(0).getLevel() && jifenCount < levelObj.get("金卡").get(0).getLevel()) {
                map.put("url", levelObj.get("金卡").get(0).getUrl());
                map.put("level", "金卡");
                map.put("percent", levelObj.get("金卡").get(0).getPercent());

            } else if (jifenCount >= levelObj.get("金卡").get(0).getLevel()) {
                map.put("url", levelObj.get("黑卡/荣誉会员").get(0).getUrl());
                map.put("level", "黑卡/荣誉会员");
                map.put("percent", levelObj.get("黑卡/荣誉会员").get(0).getPercent());

            } else {
                map.put("url", levelObj.get("白卡").get(0).getUrl());
                map.put("level", "白卡");
                map.put("percent", levelObj.get("白卡").get(0).getPercent());

            }
        } else {
            map.put("url", levelObj.get("白卡").get(0).getUrl());
            map.put("level", "白卡");
            map.put("percent", levelObj.get("白卡").get(0).getPercent());

        }

        return map;
    }


    @Override
    public int deleteByPrimaryKey(Integer id) {
        return 0;
    }

    @Override
    public int insert(SaleLevel record) {
        return 0;
    }

    @Override
    public int insertSelective(SaleLevel record) {
        return dao.insertSelective(record);
    }

    @Override
    public SaleLevel selectByPrimaryKey(Integer id) {
        return dao.selectByPrimaryKey(id);
    }

    @Override
    public List<SaleLevel> selectAll() {
        return dao.selectAll();
    }

    @Override
    public int updateByPrimaryKeySelective(SaleLevel record) {
        return dao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(SaleLevel record) {
        return 0;
    }
}
